package pprz.data_components;

import java.awt.geom.Point2D;
import java.util.Hashtable;
import java.util.Vector;

public class FlightPlan {
	
	
	
	public class Waypoint
	{
		public String name;
		public Point2D.Double location; //in m from lat0, lon0
		public double altitude; //in m from ground_alt
		public String toString() 
		{
			StringBuffer s=new StringBuffer();
			s.append(name).append("::").append("x=").append(location.x)
			.append(" y=").append(location.y).append(" z=").append(altitude);
			return s.toString();
		}
	}
	
	public class Block
	{
		public String name;
		public Vector<Stage> stages;
		public Attributes att;
		public String toString() 
		{
			StringBuffer s=new StringBuffer();
			s.append(name);
			if (att!=null) s.append("::").append(att.toString());
			return s.toString();
		}
		
	}
	
	public class Stage
	{
		public String op;
		public Attributes att;
		
		public Stage(String op) 
		{this.op=op; att=new Attributes();}
		
		public String toString() 
		{
			StringBuffer s=new StringBuffer();
			s.append(op).append("::").append(att.toString());
			return s.toString();
		}
	}
	
	public class Attributes
	{
		public Vector<String> names;
		public Vector<String> vals;
		public Attributes() 
		{
			names=new Vector<String>();
			vals=new Vector<String>();
		}
		public String toString()
		{
			StringBuffer s=new StringBuffer();
			for (int i=0; i<names.size();i++)
				s.append(names.get(i)).append("=").append(vals.get(i)).append(" ");
			return s.toString();		
		}
	}
	
	
	protected Vector<Block> blocks;
	
	protected Vector<Waypoint> waypoints;
	protected Hashtable<String, Waypoint> wp_hash;

	
	protected String name;
	protected double lat0;
	protected double lon0;
	protected GeographicConverter.UTM location;
	protected double max_dist_from_home;
	protected double qfu;
	protected double ground_alt;
	protected double security_height;
	protected double alt;

	
	public FlightPlan()
	{
		blocks=new Vector<Block>();
		waypoints=new Vector<Waypoint>();
		wp_hash=new Hashtable<String, Waypoint>();
	}
	
	public Vector<Block> getBlocks() {
		return blocks;
	}

	public void setBlocks(Vector<Block> blocks) {
		this.blocks = blocks;
	}

	public Vector<Waypoint> getWaypoints() {
		return waypoints;
	}

	public void setWaypoints(Vector<Waypoint> waypoints) {
		this.waypoints = waypoints;
	}

	public double getAlt() {
		return alt;
	}

	public void setAlt(double alt) {
		this.alt = alt;
	}

	public double getGroundAlt() {
		return ground_alt;
	}

	public void setGroundAlt(double ground_alt) {
		this.ground_alt = ground_alt;
	}

	public double getLat0() {
		return lat0;
	}

	public double getLon0() {
		return lon0;
	}

	public void setLat0Lon0(double lat0, double lon0) {
		this.lon0 = lon0;
		this.lat0=lat0;
		location=GeographicConverter.LLtoUTM(23, lat0, lon0);
	}

	public GeographicConverter.UTM getLocation() { return location;}
	
	public double getMaxDistFromHome() {
		return max_dist_from_home;
	}

	public void setMaxDistFromHome(double max_dist_from_home) {
		this.max_dist_from_home = max_dist_from_home;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getQfu() {
		return qfu;
	}

	public void setQfu(double qfu) {
		this.qfu = qfu;
	}

	public double getSecurityHeight() {
		return security_height;
	}

	public void setSecurityHeight(double security_height) {
		this.security_height = security_height;
	}

	public void addWaypoint(Waypoint wp)
	{
		waypoints.add(wp);
		wp_hash.put(wp.name, wp);
	}
	
	public void addBlock(Block bl)
	{
		blocks.add(bl);
	}
	
	public Waypoint getWp(int index)
	{
		return waypoints.get(index);
	}
	
	public Waypoint getWp(String name)
	{
		return wp_hash.get(name);
	}
	
	public Block getBlock(int index)
	{
		return blocks.get(index);
	}
	
	public Block getNewBlock() {return new Block();}
	
	public Waypoint getNewWaypoint() 
	{
		Waypoint w = new Waypoint();
		w.altitude = this.alt;
		return w;
	}
	
	public Stage getNewStage(String op) {return new Stage(op);}
	
	public GeographicConverter.UTM getHome() 
	{
		Waypoint wp=wp_hash.get("home");
		if (wp==null) wp=wp_hash.get("HOME");
		if (wp==null) return location;
		else { 
			GeographicConverter.UTM l = new GeographicConverter.UTM();
			l.easting=location.easting+wp.location.x;
			l.northing=location.northing+wp.location.y;
			l.zone=location.zone;
			return l;
		}
	}
	
	public GeographicConverter.UTM getWaypointLoc(int index) 
	{
		if (index>=waypoints.size()) return null;
		Waypoint wp=waypoints.get(index);
		GeographicConverter.UTM l = new GeographicConverter.UTM();
		l.easting=location.easting+wp.location.x;
		l.northing=location.northing+wp.location.y;
		l.zone=location.zone;
		return l;
	}
	
}
